<template>
  <div>
    <my-header></my-header>
    <!-- 这里将父组件的参数传递给子组件 -->
    <carrousel :list="swiperList"></carrousel>
    <icons :list="iconList"></icons>
    <Recommend :list="recommendList"></Recommend>
    <weekend :list="weekendList"></weekend>
  </div>
</template>

<script>
import {mapState} from 'vuex'
import MyHeader from './components/Header'
import carrousel from './components/swiper'
import Icons from './components/icons'
import Recommend from './components/Recommend'
import weekend from './components/weekend'
import axios from 'axios'
export default {
  name: 'Home',
  data () {
    return {
      swiperList: [],
      iconList: [],
      weekendList: [],
      recommendList: [],
      lastCity: ''
    }
  },
  components: {
    MyHeader,
    carrousel,
    Icons,
    Recommend,
    weekend
  },
  mounted() {
    this.lastCity = this.city
    this.getHomeInfo()
  },
  activated () {
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getHomeInfo()
    }
    console.log('activated')

  },
  methods: {
    getHomeInfo () {
      // 这里用到了代理 在config index.js
      axios.get('/api/index.json?city='+this.city).then(this.getHomeSuccess)
    },
    getHomeSuccess (res) {
      let response = res.data
      if (response.ret && response.data) {
        this.swiperList = response.data.swiperList
        this.iconList = response.data.iconList
        this.recommendList = response.data.recommendList
        this.weekendList = response.data.weekendList
      }
    }
  },
  computed: {
    ...mapState(['city'])
  }
}
</script>

<style scoped>
</style>
